﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Admin.Common;
using FortuneHeavenBAL;

namespace FortuneHeaven.Admin.Pages.Shop
{
    public partial class GiftAudit : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get { return "F24"; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDeptName.Text = "兑奖审核";
            this.usPager.PagerChanged += new Admin.Controls.Pagination.PagerChangeHandler(usPager_PagerChanged);
            if (!this.IsPostBack)
            {
                this.drpPagerSize.Items.Clear();
                this.drpPagerSize.Items.Add(new ListItem("每页10行", "10"));
                this.drpPagerSize.Items.Add(new ListItem("每页20行", "20"));
                this.drpPagerSize.Items.Add(new ListItem("每页50行", "50"));

                if (!string.IsNullOrEmpty(Request["orderid"]))
                {
                    this.txtOrderID.Text = Request["orderid"];
                }
               

                if (!string.IsNullOrEmpty(Request["pagesize"]))
                {
                    this.drpPagerSize.SelectedIndex = int.Parse(Request["pagesize"]);
                }
                else
                {
                    this.drpPagerSize.SelectedValue = "10";
                }
                
                if (!string.IsNullOrEmpty(Request["current"]))
                {
                    ResetList(int.Parse(Request["current"]));
                }
                else
                {
                    ResetList();
                }
                //this.lvNews.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvNews_ItemCommand);
               
            }
        }
        private void usPager_PagerChanged(object sender, int currentpage)
        {
            Refresh();
        }

        public void Refresh()
        {
            GiftOrderBO giftorder = new GiftOrderBO();
            lvNews.DataSource = giftorder.GetGiftOrder(this.usPager.CurrentPageIndex, int.Parse(this.drpPagerSize.SelectedValue), this.txtOrderID.Text);
            lvNews.DataBind();
        }

        protected void drpPagerSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        public void ResetList()
        {
            ResetList(1);
        }

        public void ResetList(int current)
        {
            GiftOrderBO giftorder = new GiftOrderBO();
            this.usPager.RecordCount = giftorder.GetTotalSize(this.txtOrderID.Text);
            this.usPager.PageSize = int.Parse(this.drpPagerSize.SelectedValue);
            this.usPager.Initial();
            this.usPager.CurrentPageIndex = 1;
        }

        public void AuditItem(object sender, EventArgs arg)
        {
            GiftOrderBO giftorder = new GiftOrderBO();
            string ids = "";
            foreach (ListViewItem item in this.lvNews.Items)
            {
                Control control = item.FindControl("chkSelect");

                if (control != null && control is CheckBox)
                {
                    if ((control as CheckBox).Checked)
                    {
                        ids += (item.FindControl("hfIDs") as HiddenField).Value + ",";
                    }

                }
                //需要修改

            }
            ids = ids.TrimEnd(',');
            if (!string.IsNullOrEmpty(ids))
            {
                //Need add the audit user
                giftorder.AuditItems(ids, AuthorityCenter.CurrentUserID);
                //OpLog.WriteLog(string.Format("修改礼物ID:{0}", ids));
                Refresh();
            }
        }

        public void SearchValue(object sender, EventArgs e)
        {
            ResetList();
        }

        public void OpenDetail(object sender, EventArgs args)
        {
            LinkButton button = sender as LinkButton;
            Response.Redirect(string.Format("GiftAuditDetail.aspx?id={0}&pre={1}", button.CommandArgument, "GiftAudit.aspx"));
        }

        public string Generatepre()
        {
            string ret = string.Format("Gift.aspx?pagesize={0}&current={1}&orderid={2}", this.usPager.PageSize.ToString(), this.usPager.CurrentPageIndex.ToString(), this.txtOrderID.Text);
            return Server.UrlEncode(ret);
        }
    }
}